﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1.View
{
    class BallView
    {
        private Model.BallSimulation _ballSimulation;
        private Camera _camera;

        private Texture2D _ballTexture;
        private Texture2D _arenaTexture;
        private int _resolutionX;
        private int _resolutionY;

        public BallView(Model.BallSimulation ballSimulation, GraphicsDeviceManager gdm, int resolutionX = 640, int resolutionY = 640) 
        {
            _ballSimulation = ballSimulation;            
            _resolutionX = resolutionX;
            _resolutionY = resolutionY;
            _camera = new Camera(resolutionX, resolutionY);

            resize(gdm);
        }

        public void LoadContent(ContentManager contentManager)
        {
            _ballTexture = contentManager.Load<Texture2D>("ball");
            _arenaTexture = contentManager.Load<Texture2D>("ram");
        }

        public void Draw(SpriteBatch spritebatch)
        {
            //Vector2 frameOffset = new Vector2(m_camera.m_offset, m_camera.m_offset);
            float offsetX = _camera._offsetX;
            float offsetY = _camera._offsetY;
            int resolutionX;
            int resolutionY;

            Rectangle arenaRect = new Rectangle((int)offsetX / 2, (int)offsetY / 2,
                (int)(_camera.GetVisualCoords(new Vector2(1, 1), true).X),
                (int)(_camera.GetVisualCoords(new Vector2(1, 1), true).Y));
            //spritebatch.Draw(m_arenaTexture, new Rectangle(0, 0, m_resolutionX, m_resolutionY), Color.White);
            spritebatch.Draw(_arenaTexture, arenaRect, Color.White);

            Vector2 visualCoords = _camera.GetVisualCoords(_ballSimulation.m_ball.m_position, true);
            int ballSizeX = (int)(_ballSimulation.m_ball.m_diameter * (_resolutionX - offsetX));
            int ballSizeY = (int)(_ballSimulation.m_ball.m_diameter * (_resolutionY - offsetY));
            Rectangle ballRect = new Rectangle((int)(visualCoords.X + offsetX / 2),
                (int)(visualCoords.Y + offsetY / 2),
                ballSizeX, ballSizeY);
            spritebatch.Draw(_ballTexture, ballRect, Color.White);
        }

        private void resize(GraphicsDeviceManager gdm)
        {
            gdm.IsFullScreen = false;
            gdm.PreferredBackBufferWidth = _resolutionX;
            gdm.PreferredBackBufferHeight = _resolutionY;
            gdm.ApplyChanges();
        }
    }
}
